package com.tueye.web.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tueye.entity.Admin;
import com.tueye.entity.user.User;
import com.tueye.service.usermanager.IRegisterService;
import com.tueye.utils.DateUtils;
import com.tueye.utils.TuEyeConstant;
import com.tueye.utils.UUIDGenerator;
import com.tueye.utils.servlet.CaptchaServiceSingleton;
import com.tueye.web.controller.BaseController;

@SuppressWarnings("unchecked")
@org.springframework.stereotype.Controller
@RequestMapping("/userlogin.do")
public class UserLoginController extends BaseController {
	private Log log = LogFactory.getLog(UserLoginController.class);
	
	@Autowired
	@Qualifier("registerService")
	private IRegisterService registerService;
 
 
	@RequestMapping(params = "action=login")
	public String login (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String Email = (String) request.getParameter("Email");
		String password = (String) request.getParameter("password1");
		Email = java.net.URLDecoder.decode(Email , "UTF-8");//解码
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		 
		List list=registerService.getUserLogin(Email);
     
		if(list.size()==0){ 
			   response.getWriter().write("0");//此值jquery可以接收到   
			  }else if(list.size()==1){
				  //没有用户名
			   User user = (User) list.get(0);
			   String password2 = user.getPassword();
			   if(password.equals(password2)){
				   //成功
				   
				  // response.getWriter().write("1"); 
				   request.getSession().setAttribute(TuEyeConstant.TUEYE_USER, user);
				 //tiao_url=http://www.baolex.com/centerShop.do
				    String tiao_url = request.getHeader("Referer");
//				  
				    if(tiao_url != null){ 
				    	response.getWriter().write("{target:1,yu:'"+tiao_url+"'}");
				    	System.out.println(tiao_url);
				   }
				  
			       }else{
			    	//密码不正确
        	       response.getWriter().write("2");  
        	       
			       }
			  }   
		return null; 
		
	}
	@RequestMapping(params = "action=loginout")
	public String loginout (HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.getSession().removeAttribute(TuEyeConstant.TUEYE_USER);
	//	response.sendRedirect(request.getContextPath()+"/index.html");
		return null;
	}
 

}
